
#include <QtTest/QTest>

#include "Writer.h"

library::Writer::Writer(Shelf *shelf, int time, QObject *parent) :
  shelf(shelf), time(time), QObject(parent) {}

void library::Writer::run() {

  int pos;

  while(true){

    switch(qrand() % 2) {

      case 0: {

        pos = shelf->try_write_new();

        if(pos != -1) {

          emit write();

          QTest::qSleep(time*1000);

          shelf->put_new(pos);

          collection.push_back(pos);

        }

        break;

      }

      case 1: {

        if(collection.size() == 0) 
          continue;

        pos = qrand() % collection.size();

        emit wait();

        shelf->take(pos);

        emit edit();

        QTest::qSleep(time*1000);

        shelf->put(pos);

        break;

      }

    }

  }

}

